package tk3.project.uploadservice;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.lang.Math;
import java.text.SimpleDateFormat;
import java.util.Date;



public class Picture implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6937656880945181416L;
	private File path;
	private double lon=0.0;
	private double lat=0.0;
	private int shareRadius=0;
	private int shareDuration=0;
	private byte type;//0=radius share...1=pwd share
	private String stream="";
	private String pwd="";
	private byte[] picture;
	private Date date;
	
	/**
	 * Create a picture with a GPS location
	 * @param path
	 * @param lat
	 * @param lon
	 * @param shareRadius - in which radius the picture should be shared (meters)
	 * @param shareDuration - how long the picture should be shared (hours)
	 */
	public Picture(File path, double lat, double lon, int shareRadius, int shareDuration){
		this.path=path;
		this.lon=lon;
		this.lat=lat;
		this.shareRadius=shareRadius;
		this.shareDuration=shareDuration;
		this.type=0;
	}
	/**
	 * Create a picture for a stream
	 * @param path
	 * @param stream
	 * @param password
	 * @param shareDuration - how long the picture should be shared
	 */
	public Picture(File path, String stream, String password, int shareDuration){
		this.path=path;
		this.stream=stream;
		this.pwd=password;
		this.shareDuration=shareDuration;
		this.type=1;
	}
	
	
	

	public File getPath() {
		return path;
	}

	public double getLon() {
		return lon;
	}

	public double getLat() {
		return lat;
	}

	public int getShareRadius() {
		return shareRadius;
	}

	public int getShareDuration() {
		return shareDuration;
	}
	
	public String getStream(){
		return stream;
	}
	
	public String getPassword(){
		return pwd;
	}
	
	public byte getType(){
		return type;
	}
	
	public int pictureSize(){
		return picture.length;
	}
	
	public Date getDate(){
		return date;
	}
	
	public void resetDate(){
		date=new Date();
	}
	
	/**
	 * Look if the file already exists on this location
	 * @param stdStorePath - where the file could be
	 * @return true if the picture already exists with the filename of its creation date
	 */
	public boolean isFileExisting(String stdStorePath){
		if(date==null)
			return false;
		else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS");
			String s=stdStorePath+"IMG"+sdf.format(date)+".jpg";
			File f=new File(s);
			return f.exists();
		}
	}
	
	public boolean isNewerThan(Date d){
		return date.after(d);
	}
	/**
	 * Loads the picture file into an internal array for sending the whole object through the network
	 */
	private byte[] getFile(){
		try {
			byte[] b=new byte[new Long(path.length()).intValue()];
			FileInputStream fis = new FileInputStream(path);
			fis.read(b);
			return b;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}

	
	/**
	 * Loads the picture file into an internal array for sending the whole object through the network
	 */	
	public void loadPicture(){
		picture=getFile();
	}
	/**
	 * Loads the picture file into an internal array for sending the whole object through the network
	 */
	public void unloadExistingPicture(String stdStorepath){
		if(path!=null&&path.exists()){
			picture=null;
		}
		
	}
	/**
	 * Store the new picture with the current date as filename
	 * @param stdStorepath- where to store the file
	 */
	public void unloadNewPicture(String stdStorepath){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS");
			Date d=new Date();
			if(date!=null)
				d=date;
			String s=stdStorepath+"IMG"+sdf.format(d)+".jpg";
			File f=new File(s);
			while(f.exists()){
				f=new File(stdStorepath+"IMG"+sdf.format(new Date())+".jpg");
			}
			path=f;
			writeToDisk(picture);		
	}

	/**
	 * Writes the picture from the memory to the HDD
	 * @param b
	 */
	private void writeToDisk(byte[] b){
		try {
			//System.out.println("Writenew pic to disk"+ b.length+ " "+path);
			FileOutputStream fw=new FileOutputStream(path);
			BufferedOutputStream bos=new BufferedOutputStream(fw);
			
			bos.write(b);
			bos.flush();
			bos.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * how far is the current location from the specified location
	 * @param lat
	 * @param lon
	 * @return distance in meter
	 */
	public int distance(double lat, double lon){	
		double tmp = Math.sin(this.lat*Math.PI/180) * Math.sin(lat*Math.PI/180) + Math.cos(this.lat*Math.PI/180)
				* Math.cos(lat*Math.PI/180)
				* Math.cos(lon*Math.PI/180 - this.lon*Math.PI/180);

		tmp = Math.acos(tmp)* 6378388;
		return (int)tmp ;

	}
	/**
	 * is the current picture in this specified stream?
	 * @param stream
	 * @param password
	 * @return
	 */
	public boolean isInStream(String stream, String password){
		if(this.stream.equals(stream)&&pwd.equals(password)){
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @return true if the file is still existing on the HDD
	 */
	public boolean pictureStillExists(){
		if(path.exists()){
			return true;
		}
		return false;
	}
	
	/**
	 * Erases the picture from the HDD
	 * @return
	 */
	public boolean erasePicture(){
		return path.delete();
	}

	
	
	public static void main(String[] args){
		Picture p= new Picture(new File("IMG.JPG"), 50.9, 6.9, 3, 4);
		System.out.println(p.distance(50.9, 6.9004));
		//Picture p2=new Picture(new File("IMGRET.JPG"),p.getBytes());
		//System.out.println(p2.getLon()+" "+p2.getLat()+" "+p2.getShareRadius()+" "+p2.getShareDuration());
	}
	
	
}
